﻿@{
    PageData["Title"] = "Register an Account";

    // Initialize general page variables
    var email = "";
    var password = "";
    var confirmPassword = "";
    
    // Validation
    var isValid = true;
    var emailErrorMessage = "";
    var passwordErrorMessage = "";
    var confirmPasswordMessage = "";
    var accountCreationErrorMessage = "";

    // If this is a POST request, validate and process data
    if (IsPost) {
        email = Request.Form["email"];
        password = Request.Form["password"];
        confirmPassword = Request.Form["confirmPassword"];
               
        // Validate the user's email address
        if (email.IsEmpty()) {
            emailErrorMessage = "You must specify an email address.";
            isValid = false;
        }
        
        // Validate the user's password and password confirmation
        if (password.IsEmpty()) {
            passwordErrorMessage = "The password cannot be blank.";
            isValid = false;
        }
        
        if (password != confirmPassword) {
            confirmPasswordMessage = "The new password and confirmation password do not match.";
            isValid = false;
        }
        
        // If all information is valid, create a new account
        if (isValid) { 
            // Insert a new user into the database
            var db = Database.Open("bakery");

            // Check if user already exists
            var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
            if (user == null) {
                // Insert email into the profile table
                db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);
            
                // Create and associate a new entry in the membership database.
                // If successful, continue processing the request
                try {                    
                    WebSecurity.CreateAccount(email, password, false);                                                        
                    Response.Redirect("~/");                 
                } catch (System.Web.Security.MembershipCreateUserException e) {
                    isValid = false;
                    accountCreationErrorMessage = e.ToString();
                }
            } else {
                // User already exists
                isValid = false;
                accountCreationErrorMessage = "Email address is already in use.";
            }
        }    
    }
}

<h2>Sign-up Form</h2>
<p>
   Use the form below to create a new account. 
</p>

@* If at least one validation error exists, notify the user *@
@if (!isValid) {
   <div class="message error icon">
    @if (accountCreationErrorMessage.IsEmpty()) {
        @:Please correct the errors and try again.
    } else {
        @accountCreationErrorMessage
    }
   </div>
}

<fieldset class="prettyForm">   
    <form method="post" action="">
        <div>
            <label for="email" id="labelEmail">Email:</label>
            <input type="text" id="email" name="email" value="@email" />
            @* Write any email validation errors to the page *@
            @if (!emailErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @emailErrorMessage</span>
            }
        </div>                       
        <div>
            <label for="password" id="labelPassword">Password:</label>
            <input type="password" id="password" name="password" value="" />
            @* Write any password validation errors to the page *@
            @if (!passwordErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @passwordErrorMessage </span>
            }
        </div>                       
        <div>
            <label for="confirmPassword" id="labelConfirmPassword">Confirm Password:</label>
            <input type="password" id="confirmPassword" name="confirmPassword" value="" />
            @* Write any password validation errors to the page *@
            @if (!confirmPasswordMessage.IsEmpty()) {
                <span class="message error">&raquo; @confirmPasswordMessage</span>
            }
        </div>                
        <div>
            <input type="submit" value="Register" />
        </div>
   </form>
</fieldset>
